/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.Color;

import javax.swing.JCheckBox;

import org.aergia.vinny.core.ApiResult;
import org.aergia.vinny.database.pojo.VinnyUser;
import org.aergia.vinny.database.pojo.Workstation;
import org.aergia.vinny.gui.controller.VinnyController;
import org.aergia.vinny.gui.util.FontUtil;
import org.aergia.vinny.gui.util.LabelPanel;
import org.aergia.vinny.gui.util.TextField;
import org.aergia.vinny.gui.view.EditorDialog.EditorPanel;

public class WorkstationEditor extends EditorPanel {

	private static final long serialVersionUID = 1L;

	private JCheckBox cardActive;
	private TextField commentField;
	private final VinnyController controller;
	private final VinnyUser user;
	private Workstation workstation;
	private TextField workstationNameField;

	public WorkstationEditor(final VinnyController controller, final VinnyUser user) {
		this(controller, user, null);
	}

	/**
	 * @param controller
	 * @param volunteer
	 * @param workstation
	 */
	public WorkstationEditor(final VinnyController controller, final VinnyUser user, final Workstation workstation) {
		this.controller = controller;
		this.user = user;
		this.workstation = workstation;
		this.initComponents();
	}

	@Override
	public String getTitle() {
		return "Workstation Editor";
	}

	public Workstation getWorkstation() {
		return this.workstation;
	}

	@Override
	public boolean saveAction() {
		if (this.workstation == null) {
			this.workstation = new Workstation();
		}

		if (!this.verify()) {
			return false;
		}

		this.workstation.setActive(this.cardActive.isSelected());
		this.workstation.setName(this.workstationNameField.getText());
		this.workstation.setComment(this.commentField.getText());
		this.saveWorkstation();
		return true;
	}

	private void initComponents() {

		this.cardActive = new JCheckBox();
		this.cardActive.setName("Active?");
		this.cardActive.setSelected(this.workstation != null ? this.workstation.isActive() : true);

		this.workstationNameField = new TextField(this.workstation != null ? this.workstation.getName() : "");
		this.workstationNameField.setName("Workstation Name");

		this.commentField = new TextField(this.workstation != null ? this.workstation.getComment() : "");
		this.commentField.setName("Comment");

		this.add(new LabelPanel(FontUtil.getLabelFont(), Color.black, this.cardActive, this.workstationNameField, this.commentField));
		this.setVisible(true);
	}

	private void saveWorkstation() {
		final ApiResult<Workstation> result = this.controller.saveWorkstation(this.workstation, this.user);
		if (!result.isError()) {
			this.workstation = result.getResult();
		}
	}

	private boolean verify() {
		return true;

	}
}
